/**
 * 定义污水厂管理模块
 */
(function(window, angular, undefined){
	var PsWwtpAttrManagerModule = angular.module('water.apps.ws.stationMap.psWwtpAttrManager', ['water.entity.PsWwtpAttr','water.entity.monitorStation','water.entity.sitePollutants']);
	
	PsWwtpAttrManagerModule.controller('PsWwtpAttrManagerController',function($wdp,$rootScope,$scope,$PsWwtpAttrService,$monitorStationService,$sitePollutantsService){
		/**
		 * 查询条件
		 */
		$scope.queryObject={};
		/**
		 * 污水厂数据
		 */
		$scope.wwtpList={};
		/**
		 * 站点状态统计数据
		 */
		$scope.stationStatusData={};
		/**
		 * 站点数据
		 */
		$scope.stationData={};
		/**
		 * 当前选中站点数据
		 */
		$scope.model={};
		/**
		 *站点污染物参数数据
		 */
		$scope.pollList={};
		/**
		 * 站点类型污水为0
		 */
		$scope.queryObject.WATER_TYPE='0';
		/**
		 * 站点状态
		 */
		$scope.queryObject.stationStatus='';
		/**
		 * 站点ID
		 */
		$scope.queryObject.STATION_ID="";
		/**
		 * 主动通知timeout
		 */
		$scope.callNoticeParmChange = null;
		/**
		 * 控制参数展开与收起
		 */
		$scope.parmListShow=false;
		/**
		 * 获取站点状态统计数据
		 */
		function getstationStatusData(){
			$monitorStationService.getList($scope.queryObject).then(function(d){
				$scope.stationData = d;
				$scope.getPsWwtpAttr();
			});
			$monitorStationService.getStatusStatistics($scope.queryObject).then(function(d){
				$scope.stationStatusData = d;
			});
			
		};
		/**
		 * 获取污染物参数数据
		 */
		function getPollList(){
			$sitePollutantsService.getSitePollutantsParam($scope.queryObject).then(function(d){
				$scope.pollList = d;
			});
		};
		/**
		 * 获取污水厂处理厂数据
		 */
		$scope.getPsWwtpAttr=function(){
			$PsWwtpAttrService.getList($scope.queryObject).then(function(d){
				$scope.wwtpList = d.data;
				for(var i=0;i<$scope.stationData.length;i++){
					var obj=$scope.stationData[i];
					for(var j=0;j<$scope.wwtpList.length;j++){
						var w=$scope.wwtpList[j];
						if(w.EXP_NO=='WS020200438599'){
							if(obj.EXP_NO=='WS020200500050'){
								$scope.wwtpList[j].IN_DATA_ONE=obj;
							}
                            if(obj.EXP_NO=='WS020200500051'){
                            	$scope.wwtpList[j].IN_DATA_TWO=obj;
							}
                            if(obj.EXP_NO=='WS020200500092'){
                            	$scope.wwtpList[j].OUT_DATA=obj;
							}
						}
						else{
							if(obj.WWTP_ID==w.EXP_NO&&obj.FLOW_TYPE=='0'){
								$scope.wwtpList[j].IN_DATA=obj;
							}
							if(obj.WWTP_ID==w.EXP_NO&&obj.FLOW_TYPE=='1'){
								$scope.wwtpList[j].OUT_DATA=obj;
							}
						}
						
					}
				}
				$scope.model=$scope.wwtpList[0]['IN_DATA'];
				$scope.queryObject.STATION_ID=$scope.wwtpList[0]['IN_DATA'].EXP_NO;
				getPollList();
			});
			
		};
		/**
		 * 参数展开与收起
		 */
		$scope.onParmListClick=function(b){
			$scope.parmListShow=b;
			if(b==false){
				$("#reportDataList").height($rootScope.Map_H-140)
			}
			if(b==true){
				$("#reportDataList").height($rootScope.Map_H-252)
			}
		}
		/**
		 * 参数全不选
		 */
		$scope.unSelectParms = function(){
			if($("#selectAll").val()=="全选"){
				$("#selectAll").attr("value",'全不选');
				$("input[name='parmToCheck']").attr("checked",true);
				noticeParmChange();
				$wdp.stationListReady = "true";
			}
			else if($("#selectAll").val()=="全不选"){
				$("#selectAll").attr("value",'全选');
				$("input[name='parmToCheck']").attr("checked",false);
			}
		}
		/**
		 * 获取界面上勾选的参数
		 */
		function getReportParms(){
			if(!$scope.model){
				return;
			}
			var parmToCheckDom = $("input[name='parmToCheck']:checked");
			var parmToChecksStr = "";
			var parmToCheckList = [];
			
			for(var i=0;i<parmToCheckDom.length;i++){
				var parmCode = $(parmToCheckDom[i]).val();
				var parmText = "";
				var parmName = $(parmToCheckDom[i]).attr("parmText");
				var parmUnit = $(parmToCheckDom[i]).attr("UNIT");
				var parmCouUnit= $(parmToCheckDom[i]).attr("couunit");
				if(parmUnit){
					parmText = parmName + "(" + parmUnit + ")";
					parmCouText=parmName + "(" + parmCouUnit + ")";
				}
				else{
					parmText = parmName;
					parmCouText = parmName;
				}
				parmToChecksStr += $(parmToCheckDom[i]).val()+",";
				parmToCheckList.push({"name_original":parmCode,"name":parmCode.toUpperCase(),"text":parmText,parmName:parmName,"couunit":parmCouUnit,"unit":parmUnit});
			}
			$.cookie("parmToChecksStr",parmToChecksStr,{expires:7});
			var wwtpReportParm = {
						parmToChecksStr:parmToChecksStr,
						parmToCheckList:parmToCheckList,
						STATION_ID:$scope.model.CODE,
						STATION_NAME:$scope.model.NAME,
						MN:$scope.model.DEVICE_NUM
					};
			
			if($scope.pollList == null || $scope.pollList.length <1){
				wwtpReportParm.parmToChecksStr = "";
				wwtpReportParm.parmToCheckList = [];
			}
			
			return wwtpReportParm;
		};
		/**
		 * 通知参数变化
		 */
		function noticeParmChange(){
			
			try {
				if($scope.callNoticeParmChange){
					clearTimeout($scope.callNoticeParmChange);
				}
			} catch (e) {
			}
			
			$scope.reportParms = getReportParms();
			$scope.$emit('dataPramChange', $scope.reportParms); 
		}
		/**
		 * 站点参数加载完成执行
		 */
		$scope.pollLoadFinish = function(){
			noticeParmChange();
			$wdp.stationListReady = "true";
		};
		/**
		 * 站点点击事件
		 */
		$scope.onStationClick=function(data){
			$scope.queryObject.STATION_ID=data.EXP_NO;
			$scope.model=data;
			getPollList();
		};
		/**
		 * 查询事件
		 */
		$scope.onClickquery=function(){
			$scope.getPsWwtpAttr();
		}
		$scope.$on("$stateChangeStart",function(){
			$scope.callNoticeParmChange = setTimeout(function(){
				noticeParmChange();
			},500);
		});
		//初始化获取站点状态统计和站点数据
		getstationStatusData();
		
		
	});
}(window, angular));
